package priv.pront.code.robot.roboCom_na_2022;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-06-25 12:09
 */
public class RC_v4_情人节的蜜语 {

    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    static boolean flag = false;
    static List<Info> list = new ArrayList<>();
    static List<Info> ans = new ArrayList<>();
    static int n, m, idx = 0;
    static char[][] g, gg;
    static boolean[][] st;
    static char[] target;
    static int[] dx = {-1, -1, 0, 1, 1, 1, 0, -1}, dy = {0, 1, 1, 1, 0, -1, -1, -1};

    static class Info{
        public int row;
        public int col;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Info info = (Info) o;
            return row == info.row && col == info.col;
        }

        @Override
        public int hashCode() {
            return Objects.hash(row, col);
        }

        public Info(int row, int col) {
            this.row = row;
            this.col = col;
        }
    }

    public static void main(String[] args) throws IOException {
        String[] fLine = bf.readLine().split(" ");
        n = Integer.parseInt(fLine[0]);
        m = Integer.parseInt(fLine[1]);
        g = new char[n][m];
        gg = new char[n][m];
        st = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            String s = bf.readLine();
            g[i] = s.toCharArray();
            gg[i] = s.toCharArray();
        }
        target = bf.readLine().toCharArray();

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (g[i][j] != target[idx]) continue;
                dfs(i, j, 1);
            }
        }

        while (ans.size() != target.length) {
            ans.remove(0);
        }

        for(int i = 0; i < n; i++){
            for (int j = 0; j < m; j++){
                if (!ans.contains(new Info(i, j))) {
                    g[i][j] = '.';
                }
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                out.print(g[i][j]);
                out.flush();
            }
            out.println();
        }
    }

    private static void dfs(int row, int col, int idx) {
        if (idx == target.length && !flag) {
            copy();
            flag = true;
            return;
        }
        if(idx == 1){
            list.add(new Info(row, col));
        }
        for (int i = 0; i < 8; i++) {
            int nx = row + dx[i], ny = col + dy[i];
            if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
            if (st[nx][ny]) continue;
            if (g[nx][ny] == target[idx]) {
                st[nx][ny] = true;
                list.add(new Info(nx, ny));
                dfs(nx, ny, idx + 1);
                st[nx][ny] = false;
                list.remove(list.size() - 1);
            }
        }
    }

    private static void copy() {
        for (Info ints : list) {
            Info info = new Info(ints.row, ints.col);
            ans.add(info);
        }
    }
}
